<template>
  <div class="card-wrapper" @click="goToDetail">
    <!-- 删除按钮 -->
    <el-icon
      v-if="props.delete"
      class="delete"
      @click.stop="$emit('deleteClick')"
    >
      <close-bold></close-bold>
    </el-icon>
    <!-- 删除按钮 -->

    <!-- 图片 -->
    <img :src="url" class="face" />
    <!-- 图片 -->

    <div class="info">
      <!-- 标题 -->
      <div class="title">{{ title }}</div>
      <!-- 标题 -->
      <div class="user-info">
        <div class="name">
          <!-- 头像 -->
          <avatar class="avatar" :avatar="userInfo.avatar"></avatar>
          <!-- 头像 -->
          <p>
            {{ userInfo.name }}
          </p>
        </div>
        <div class="fav">
          {{ dayjs(createDate).format('YYYY-MM-DD hh:mm:ss') }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import dayjs from 'dayjs';
import { useRouter } from 'vue-router';
import Avatar from '../avatar/avatar.vue';
import { CloseBold } from '@element-plus/icons-vue';
const props = defineProps({
  id: {
    type: String,
    required: true,
  },
  url: {
    type: String,
    required: true,
  },
  title: {
    type: String,
    required: true,
  },
  favorite: {
    type: Number,
    required: true,
  },
  createDate: {
    type: Number,
  },
  delete: {
    type: Boolean,
    default: false,
  },
  userInfo: {
    type: Object as () => { name: string; avatar: string },
  },
});
defineEmits(['deleteClick']);
const router = useRouter();

// 跳转详情
const goToDetail = () => {
  router.push({ path: '/detail', query: { id: props.id } });
};
</script>

<style lang="scss" scoped>
.card-wrapper {
  cursor: pointer;
  overflow: hidden;
  position: relative;
  height: max-content;
  border-radius: 10px;
  border: 1px solid rgb(0 0 0 / 10%);

  .delete {
    top: 5px;
    right: 5px;
    z-index: 1000;
    font-size: 15px;
    position: absolute;
    color: var(--white-color);
  }

  .info {
    padding: 10px;

    .title {
      width: 100%;
      overflow: hidden;
      font-size: 16px;
      white-space: nowrap;
      text-overflow: ellipsis;
    }

    .user-info {
      display: flex;
      font-size: 14px;
      white-space: nowrap;
      align-items: center;
      justify-content: space-between;

      .fav {
        line-height: 18px;
      }

      .name {
        display: flex;
        line-height: 28px;
        align-items: center;

        p {
          width: 80px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }

        .avatar {
          width: 30px;
          height: 30px;
          flex-shrink: 0;
          margin-right: 10px;
        }
      }
    }
  }

  .face {
    width: 100%;
    height: 300px;
    object-fit: cover;
  }

  &:hover {
    &::after {
      top: 0;
      width: 100%;
      height: 100%;
      z-index: 999;
      content: '';
      position: absolute;
      background-color: rgba(0, 0, 0, 0.2);
    }
  }
}
</style>
